<template>
  <el-container>
    <el-header>
      <el-row>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <img src="../assets/logo2.jpg" alt="" />
          </div>
        </el-col>
        <el-col :span="16">
          <div class="grid-content bg-purple-light" @click="getUsers">
            电商后台管理系统
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-dropdown @command="outs">
              <span class="el-dropdown-link">
                {{ userinfo.username }}
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </el-header>
    <el-container class="selected">
      <el-aside width="200px">
        <el-menu
          default-active="2"
          class="el-menu-vertical-demo"
          :unique-opened="true"
          :router="true"
        >
          <el-submenu
            :index="item.path"
            v-for="(item, index) in menusdata"
            :key="index"
          >
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item-group>
              <el-menu-item
                :index="val.path"
                v-for="(val, key) in item.children"
                :key="key"
                >{{ val.authName }}</el-menu-item
              >
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
//引入css样式
import "../assets/style.css";
export default {
  data() {
    return {
      userinfo: {},
      menusdata: [],
    };
  },

  async mounted() {
    //getitem使用login传递的数据
    // const userinfo = localStorage.getItem("userdata");
    // const userinfo = this.$store.state.userinfo
    //获取用户数据
    this.userinfo = JSON.parse(localStorage.getItem("userinfo"));

    //获取侧边栏数据 进行遍历
    const { data } = await this.$axios.get("/menus");
    if (data.meta.status == 200) {
      this.menusdata = data.data;
      console.log(data);
    } else {
      this.$message.error(data.meta.msg);
    }
  },
  //判断用户身份不能再缓冲
  //需要在跳转之后，组件加载之间，进行用户身份的验证
  // 这就需要路由中的导航守卫

  methods: {
    // changeState() {
    //   // this.$store.commit("adds");
    //   console.log(this.$store.state.counts);
    // },
    //获取用户信息
    getUsers() {
      this.$axios
        .get(`/users?pagenum=1&pagesize=1`)
        .then((serverBackData) => {
          const { data } = serverBackData.data;
          // console.log(data);
        })
        .catch((err) => {});
    },
    //退出登录
    outs() {
      this.$message({
        message: "退出成功",
        type: "scuess",
      });
      localStorage.removeItem("userinfo");
      this.$router.push({ path: "/Login" });
    },
  },
};
</script>

<style>
.selected {
  height: 550px;
}
.el-header,
.el-footer {
  background-color: #fff;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-aside {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  line-height: 200px;
}

.el-main {
  background-color: #ffffff;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}
img {
  width: 50px;
}
</style>